﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;

namespace MapControlDemo
{
    public class LocationToPointConverter : IValueConverter
    {
        public static readonly LocationToPointConverter Instance = new LocationToPointConverter();
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                BuisnessData.GeographicLocation loc = (BuisnessData.GeographicLocation)value;
                return new Point(loc.Lon, loc.Lat);
            }
            catch (InvalidCastException) { }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
        
            try
            {
                Point p;
                if (value is string)
                {
                    p = Point.Parse((string)value);
                }
                else{
                     p = (Point)value;
                }
                return new BuisnessData.GeographicLocation(p.X, p.Y);
            }
            catch (InvalidCastException) { }
            return null;
        }

        #endregion
    }
}
